package route

import (
	"fmt"
	"os"

	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/os/gres"
	"github.com/gogf/gf/v2/os/gspath"
)

func ReadFile(path string) []byte {
	f := gres.GetWithIndex(path, []string{})
	if f == nil {
		wd, err := os.Getwd()
		if err != nil {
			panic(err)
		}
		p, isDir := gspath.Search(wd, path)
		if p != "" && !isDir {
			return gfile.GetBytes(p)
		}
	} else {
		defer f.Close()
		return f.Content()
	}
	panic(gerror.New(fmt.Sprintf("file not found: %s", path)))
}
